﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eRechnung.Properties;

namespace eRechnung
{
    public partial class FrmUIDBestaetigung : Form
    {
        public string UID_Biller;
        public string UID_Receiver;
        public string Teilnehmer;
        public string Benutzer;

        public FrmUIDBestaetigung()
        {
            InitializeComponent();
            txUIDReceiver.Text = UID_Receiver;
            txUIDBiller.Text = UID_Biller;
            txBenutzer.Text = Benutzer;
            txTeilnehmer.Text = Teilnehmer;
        }

        private void ShowMsg(string text)
        {
            MessageBox.Show(text, "UID Bestätigung", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private bool CheckNotEmpty(string field, string text)
        {
            if (field == "")
            {
                ShowMsg("Das Feld " + text + "darf nicht leer sein.");
                return (true);
            }
            return (false);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SessionWSIService sess;
            uidAbfrageService uabf;

            sess = new SessionWSIService();
            string sessionid;
            string pwd = txPIN.Text;
            string tid = txTeilnehmer.Text;
            string benid = txBenutzer.Text;
            string uid_tn = txUIDBiller.Text; // "ATU62698637";
            sessionid = sess.Login(tid, benid, pwd);

            uabf = new uidAbfrageService();
            string erg;

            string uid = txUIDReceiver.Text; // "ATU54047801";
            string stufe = "2";
            string name;
            string adrz1;
            string adrz2;
            string adrz3;
            string adrz4;
            string adrz5;
            string adrz6;
            erg = uabf.uidAbfrage(sessionid, tid, benid, uid_tn, uid, stufe, out name, out adrz1, out adrz2, out adrz3, out adrz4, out adrz5, out adrz6);

            eRechnung.FinanzOnline.ErrorCodes ecTab = new FinanzOnline.ErrorCodes();

            string ec_text = ecTab.ErrorText(erg);
            if (erg!="0")
            {
                MessageBox.Show(ec_text, "UID Bestätigung", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string adrz = string.Empty;
            adrz = adrz + ((adrz1 != "") ? Environment.NewLine + adrz1 : "");
            adrz = adrz + ((adrz2 != "") ? Environment.NewLine + adrz2 : "");
            adrz = adrz + ((adrz3 != "") ? Environment.NewLine + adrz3 : "");
            adrz = adrz + ((adrz4 != "") ? Environment.NewLine + adrz4 : "");
            adrz = adrz + ((adrz5 != "") ? Environment.NewLine + adrz5 : "");
            adrz = adrz + ((adrz6 != "") ? Environment.NewLine + adrz6 : "");


            MessageBox.Show(ec_text + Environment.NewLine + Environment.NewLine + 
                            "Name: " + Environment.NewLine +
                            name + Environment.NewLine + Environment.NewLine + 
                            "Adresse: " + adrz);
            return;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txUIDBiller_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = CheckNotEmpty(txUIDBiller.Text,"Ihre UID");
        }


        private void txUIDReceiver_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = CheckNotEmpty(txUIDReceiver.Text,"UID des Rechnungsempfängers");
        }

        private void txTeilnehmer_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = CheckNotEmpty(txTeilnehmer.Text, "Teilnehmer-Identifikation");

        }

        private void txBenutzer_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = CheckNotEmpty(txBenutzer.Text, "Benutzer-Identifikation");
        }

        private void txPIN_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = CheckNotEmpty(txPIN.Text, "PIN");
        }

    }
}
